package svcnode.share;

public class Operate
{
    private static Operate instance=null;

    private Operate()
    {
    }

    public synchronized static Operate getInstance()
    {//{{{
        if(instance == null)
        {
            instance=new Operate();
        }
        return instance;
    }//}}}

    public void init()
    {/*{{{*/
        Cnfg cnfg=Cnfg.getInstance();

        this.shutdown=false;

        this.netlog=ThreadStatus.RUNNING;
        this.workers=new ThreadStatus[cnfg.workers];
        for(int i=0; i<this.workers.length; ++i)
        {
            this.workers[i]=ThreadStatus.RUNNING;
        }
        this.session_cleaner=ThreadStatus.RUNNING;
    }/*}}}*/

    private boolean shutdown;
    public static enum ThreadStatus { RUNNING, STOPPED };

    private ThreadStatus netlog;
    private ThreadStatus[] workers;
    private ThreadStatus session_cleaner;

    public synchronized void shutdown()
    {
        this.shutdown=true;
    }

    public synchronized boolean canShutdown()
    {
        return this.shutdown;
    }

    public synchronized boolean isShutdown()
    {/*{{{*/
        Cnfg cnfg=Cnfg.getInstance();

        if(this.netlog == ThreadStatus.RUNNING)
            return false;
        for(int worker_idx=0; worker_idx<cnfg.workers; ++worker_idx)
        {
            if(this.workers[worker_idx] == ThreadStatus.RUNNING)
                return false;
        }
        if(this.session_cleaner == ThreadStatus.RUNNING)
            return false;
        return true;
    }/*}}}*/

    public synchronized void NetlogStop()
    {
        this.netlog=ThreadStatus.STOPPED;
    }

    public synchronized void WorkerStop(int worker_idx)
    {
        this.workers[worker_idx]=ThreadStatus.STOPPED;
    }

    public synchronized void SessionCleanerStop()
    {
        this.session_cleaner=ThreadStatus.STOPPED;
    }
}
